﻿namespace WPForms.Phone.ViewModel
{
    using System;
    using System.Net;
    using System.Windows;
    using System.Windows.Controls;
    using System.Windows.Documents;
    using System.Windows.Ink;
    using System.Windows.Input;
    using System.Windows.Media;
    using System.Windows.Media.Animation;
    using System.Windows.Shapes;
    using Microsoft.Practices.Prism.Commands;
    using Microsoft.Practices.Prism.ViewModel;
    using PhoneUtils.Services;
    using WPForms.Common.Model;
    using WPForms.Common.Services;

    /// <summary>
    /// View model, which is responsible for creating new forms.
    /// </summary>
    public class CreateFormViewModel : NotificationObject
    {
        /// <summary>
        /// The form store service.
        /// </summary>
        private IFormStore formStore;

        /// <summary>
        /// The navigation service.
        /// </summary>
        private INavigationService navigationService;

        /// <summary>
        /// Initializes a new instance of the <see cref="CreateFormViewModel" /> class.
        /// </summary>
        /// <param name="formStore">The form store.</param>
        /// <param name="descriptions">The descriptions.</param>
        /// <param name="navigationService">The navigation service.</param>
        public CreateFormViewModel(IFormStore formStore, FormDescriptionCollection descriptions, INavigationService navigationService)
        {
            this.navigationService = navigationService;
            this.formStore = formStore;
            this.CreateCommand = new DelegateCommand(this.CreateForm);
            this.Descriptions = descriptions;
            if (this.Descriptions.Count > 0)
            {
                this.SelectedType = this.Descriptions[0];
            }
        }

        /// <summary>
        /// Gets or sets the name of the form to create.
        /// </summary>
        public string Name { get; set; }

        /// <summary>
        /// Gets or sets the selected form description.
        /// </summary>
        public FormDescription SelectedType { get; set; }

        /// <summary>
        /// Gets the available form descriptions.
        /// </summary>
        public FormDescriptionCollection Descriptions { get; private set; }

        /// <summary>
        /// Gets the command responsible for creating the form.
        /// </summary>
        public ICommand CreateCommand { get; private set; }

        /// <summary>
        /// Creates the form based on user data using the form store.
        /// </summary>
        private void CreateForm()
        {
            this.formStore.CreateForm(new FormInstanceDescription()
                {
                    Description = this.SelectedType.Description,
                    FormName = this.SelectedType.FormName,
                    Uri = this.SelectedType.Uri,
                    InstanceName = this.Name,
                });
            this.Name = string.Empty;

            if (this.navigationService.CanGoBack)
            {
                this.navigationService.GoBack();
            }
        }
    }
}
